package com.sixth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.BannerDao;
import com.sixth.dao.ZoneCourseDao;
import com.sixth.dao.ZoneDao;

import com.sixth.entity.*;
import com.sixth.feign.CourseFeign;
import com.sixth.service.ZoneService;
import com.sixth.until.CommonResult;
import com.sixth.until.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: self-tui-education
 * @description:
 * @author: C
 * @create: 2022-04-19 14:52
 **/
@Service
public class ZoneServicelmpl implements ZoneService {
    @Autowired
    private ZoneDao zoneDao;
    @Autowired
    private ZoneCourseDao zoneCourseDao;
    @Autowired
    private CourseFeign courseFeign;
    @Autowired
    private BannerDao bannerDao;
    @Override
    public TableResult selectAllzone(Integer pageNum, Integer pageSize,Zone zone) {
        PageHelper.startPage(pageNum,pageSize);
        String zoneName = zone.getZoneName();
        Integer isDeleted = zone.getIsDeleted();
        System.out.println(zoneName+isDeleted);
        List<Zone> eduTeachers = zoneDao.selectZoneByCondition(zoneName,isDeleted);
        PageInfo<Zone> pageInfo = new PageInfo<Zone>(eduTeachers);

        return new TableResult(2000,"查询成功",pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public CommonResult deleteFindById(Zone zone) {
          String id= zone.getId();
        int row =zoneDao.deleteByPrimaryKey(id);
        if (row>0){
            return new CommonResult(2000,"删除成功","null");
        }else {
            return new CommonResult(5000,"删除失败","null");
        }
    }


    @Override
    public CommonResult addZone(Zone zone) {
        String id=OrderNoUtil.getOrderNo();
        int i = zoneDao.addZone(id,zone);
        if (i !=0){
            return new CommonResult(2000,"添加成功",null);
        }
        return new CommonResult(2000,"添加失败",null);
    }

    @Override
    public CommonResult UpdateZone(Zone zone) {
        int i = zoneDao.updateByPrimaryKeySelective(zone);
        if (i != 0) {
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(5000,"修改失败",null);
    }

    @Override
    public CommonResult changeStatus(String id, Integer isDisable) {
        int i = zoneDao.changeStatusByid(id,isDisable);
        if (i != 0) {
            return new CommonResult(2000,"修改状态成功",null);
        }
        return new CommonResult(2000,"修改失败",null);
    }

    @Override
    public CommonResult getAllQianZone() {
        QueryWrapper<Zone> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted","0");
        wrapper.eq("is_disable","0");
        List<Zone> zones = zoneDao.selectList(wrapper);
        for (Zone zone:zones){
            List<Course> list = new ArrayList<>();
            QueryWrapper<ZoneCourse> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("zone_id",zone.getId());
            wrapper2.eq("is_deleted","0");
            wrapper2.eq("is_disable","0");
            List<ZoneCourse> zoneCourses = zoneCourseDao.selectList(wrapper2);
            for (ZoneCourse zo:zoneCourses){
                Course course = courseFeign.selectCourseById(zo.getCourseId());
                if (course!=null){
                    list.add(course);
                }
            }
            zone.setCourseList(list);
        }
        return new CommonResult(2000,"查询成功",zones);
    }
    @Override
    public CommonResult getQianBanner() {
        QueryWrapper<Banner> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted","0");
        List<Banner> banners = bannerDao.selectList(wrapper);
        if (banners!=null){
            return new CommonResult(2000,"查询成功",banners);
        }else {
            return new CommonResult(5000,"查询失败",null);
        }
    }

    @Override
    public CommonResult getAllQianMenu() {
        List<Subject> allSubject = courseFeign.findAllSubject();
        return new CommonResult(2000,"查询成功",allSubject);
    }
}
